﻿class JasmineTests {
    suiteName: string = 'Not specified';

    childSuites: Array<JasmineTests> = [];

    beforeEach(): void {
    }
    afterEach(): void {
    }

    run(): void {

        describe(this.suiteName, () => {
            beforeEach(() => {
                (<any>jasmine).addCustomEqualityTester(function (value1, value2) {
                    //EntityReference comparision
                    if (typeof value1 == 'object' && typeof value2 == 'object') {
                        if (value1.length && value2.length) {

                            if (value1.length >= 1 && value1[0].id ||
                                value2.length >= 1 && value2[0].id) {

                                //Actual check
                                if (value1.length != value2.length)
                                    return false;

                                for (var i = 0; i < value1.length; i++) {
                                    if (value1[i] && value2[i]) {
                                        if (value1[i].id != value2[i].id || value1[i].entityType != value2[i].entityType)
                                            return false;
                                    }
                                    else if (value1[i] || value2[i]) {
                                        return false;
                                    }
                                }

                                return true;
                            }
                        }
                    }
                });
                this.beforeEach();
            });

            var target: any = this;
            for (var m in target) {
                if (m.length < 6)
                    continue;

                if (m.substr(0, 5) == 'test_') {
                    this.runTest(target, m);
                }
            }

            for (var i in this.childSuites) {
                this.childSuites[i].run();
            }

            afterEach(() => {
                this.afterEach();
            });

        });

    }

    private runTest(target: any, method: string) {
        it(method.substr(5), () => {
            target[method].apply(target);
        });

    }


}